from flask import Flask, request
#coding: utf-8
import esm
import time
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

app = Flask(__name__)
tagmap = {}
exclude_basetag_list=['基本属性/建筑类型','交易属性/交易权属']
all_words = set()
index = esm.Index()
count=0
for line in open('new_tag_content.txt'):
    cols = line.strip().split('\t')
    if len(cols) != 2:
        continue
    tag, words = cols
    words = words.split(' ')
    count=count+1
    for word in words:
        tagmap.setdefault(word, [])
        tagmap[word].append(tag)
        # 添词
        index.enter(word)
# 添完所有词后，生成查询结构
index.fix()

@app.route('/extract', methods=['GET', 'POST'])
def extract():
    global index
    #house_desc = request.args.get('text')
    house_desc = request.values.get('text', 0)
    print 'house_desc='+house_desc
    # 匹配词
    mat = set([m[1] for m in index.query(house_desc)])
    mat1=mat
    newmat=set()
    tagset=set()
    exclude_dict={}
    response=''
    for w in mat:
        subword=0
        for word in mat1:
            if word!=w and w in word:
                subword=1
                break
        if subword==0:
            if tagmap[w][0] not in tagset:
                newmat.add(w)
		tagset.add(tagmap[w][0])
                tag_base=tagmap[w][0].rsplit('/',1)[0]
                if tag_base in exclude_basetag_list:
                    if tag_base not in exclude_dict:
                        exclude_dict[tag_base] = 1
                    else:
                        exclude_dict[tag_base] = exclude_dict[tag_base] + 1
    for w in newmat:
        tag_base=tagmap[w][0].rsplit('/',1)[0]
        if tag_base not in exclude_dict or exclude_dict[tag_base] ==1:
	    matchedinfo=tagmap[w][0]
	    if response == '':
		response=matchedinfo
	    else:
		response=response+';'+matchedinfo; 

    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7896)
